import PageParam from '@main/bo/PageParam'
import { ArgumentMetadata, PipeTransform } from '@nestjs/common'

export class IdsParamPipe implements PipeTransform<Record<'id', string | string[] | undefined>, number[]> {
  transform(value: Record<'id', string | string[]>, metadata: ArgumentMetadata): number[] {
    if (metadata.type === 'query') {
      return toIntArr([], value.id)
    }
    return []
  }
}

function toIntArr(defaultVal: number[], val?: string | string[]) {
  if (val) {
    if (!Array.isArray(val)) {
      const ret = toInt(val)
      return typeof ret === 'number' ? [ret] : []
    } else {
      const ret = []
      for (let item of val) {
        const tmpVal = toInt(item)
        if (typeof tmpVal === 'number') {
          ret.push(tmpVal)
        }
      }
      console.log('ret', ret)
      return ret
    }
  }
  return defaultVal
}
function toInt(val?: string) {
  if (val) {
    return parseInt(val)
  }
}
